import pygame
import random
# 定义不同种类事件的编号
from basic_class import *
class Monster(EntityLike):  # 玩家类

    def __init__(self,image: pygame.Surface, rect: pygame.Rect):
        # 继承实体类
        super().__init__(image, rect)
        self.hp = 8
    def listen(self, event: Event):  # 玩家类所响应的事件
        if event.code != pygame.KEYDOWN:

            self.move()  # 键盘按下事件
        if event.code == CAN_MOVE_MONSTER:  # 响应场景发出的允许移动事件
            self.rect.x = event.body["POS"][0]
            self.rect.y = event.body["POS"][1]
        super().listen(event)  # 继承原有的响应事件内容，如对DRAW的响应
    def move(self):
        THE_EVENT =pygame.USEREVENT + 1
        pygame.time.set_timer(THE_EVENT,1000)
        nx = self.rect.x
        ny = self.rect.y
        direction = random.choice([(0,1),(0,-1),(1,0),(-1,0)])
        if direction == (0,1):
            ny += 10
            
        elif direction == (0,-1):
            ny -= 10
           
        elif direction == (1,0):
            nx += 10
            
        elif direction == (-1,0):
            nx -= 10

        while True: 
            for event in pygame.event.get():
                if event.type == THE_EVENT:
                    self.post(Event(REQUEST_MOVE_MONSTER, {"POS": (nx, ny)}))  # 发出请求移动事件                

    